package com.atguigu.srb.core.controller.api;

import com.atguigu.srb.common.result.R;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/core/userBind")
public class ApiUserBindController {

    @Autowired
    UserBindService userBindService;

    @PostMapping("commitBind")
    public R commitBind(@RequestBody UserBindVO userBindVO, HttpServletRequest request){
        String userId = request.getHeader("userId");
        // 调用业务层，根据汇付宝api生成汇付宝提交表单
        String form = userBindService.commitBind(userBindVO,userId);
        return R.ok().data("form",form);
    }

    @PostMapping("notify")
    public String commitBindNotify(HttpServletRequest request){
        String resultCode = request.getParameter("resultCode");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> returnParamMap = RequestHelper.switchMap(parameterMap);
        // 调用业务层，根据汇付宝api生成汇付宝提交表单
        userBindService.commitBindNotify(returnParamMap);

        return"success";
    }


}
